#include "iot_errno.h"
#include "../inc/iot_gpio_ex.h"
#if CHIP_WS63
#include "pinctrl.h"
#include "los_base.h"
#else
#include "hi_gpio.h"
#include "hi_io.h"
#include "hi_task.h"
#include "hi_types_base.h"
#endif

/**
 * @brief Sets the pull-up or pull-down resistor for a specified GPIO pin.
 *
 * @param id The GPIO pin ID.
 * @param val The pull-up/pull-down configuration.
 * @return IOT_SUCCESS on success, IOT_FAILURE on failure.
 */
#if CHIP_WS63
unsigned int IoSetPull(unsigned int id, IotIoPull val)
{
    if (id >= PIN_NONE) {
        return IOT_FAILURE;
    }
    return uapi_pin_set_pull((pin_t)id, (pin_pull_t)val);
}

/**
 * @brief Sets the alternate function for a specified GPIO pin.
 *
 * @param id The GPIO pin ID.
 * @param val The alternate function value.
 * @return IOT_SUCCESS on success, IOT_FAILURE on failure.
 */
unsigned int IoSetFunc(unsigned int id, unsigned char val)
{
    if (id >= PIN_NONE) {
        return IOT_FAILURE;
    }
    return uapi_pin_set_mode((pin_t)id, val);
}


/**
 * @brief Delays the task for a specified number of milliseconds.
 *
 * @param ms The delay time in milliseconds.
 * @return IOT_SUCCESS on success, IOT_FAILURE on failure.
 */
unsigned int TaskMsleep(unsigned int ms)
{
    if (ms <= 0) {
        return IOT_FAILURE;
    }
    
    LOS_Msleep((unsigned int)ms);

    return IOT_SUCCESS;
}
#else
unsigned int IoSetPull(unsigned int id, IotIoPull val)
{
    if (id >= HI_GPIO_IDX_MAX) {
        return IOT_FAILURE;
    }
    return hi_io_set_pull((hi_io_name)id, (hi_io_pull)val);
}

unsigned int IoSetFunc(unsigned int id, unsigned char val)
{
    if (id >= HI_GPIO_IDX_MAX) {
        return IOT_FAILURE;
    }
    return hi_io_set_func((hi_io_name)id, val);
}


/**
 * @brief Delays the task for a specified number of milliseconds.
 *
 * @param ms The delay time in milliseconds.
 * @return IOT_SUCCESS on success, IOT_FAILURE on failure.
 */
unsigned int TaskMsleep(unsigned int ms)
{
    if (ms <= 0) {
        return IOT_FAILURE;
    }
    return hi_sleep((hi_u32)ms);
}
#endif